﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//using Ivy.Common.Framework.GeneralUtility;

using Hsmp.SocietyAdmin.BusinessLogic;
using Hsmp.SocietyAdmin.Entity;

using Hsmp.SocietyOperation.BusinessLogic;
using Hsmp.SocietyOperation.BusinessEntity;
using Hsmp.SocietyOperation.Entity;

using Ivy.UserRegistration.BusinessLogic;
using Ivy.UserRegistration.Entity;

using HSMPWeb.ViewEntity;
namespace HSMPWeb.Pages
{
    public partial class SocietyManagementComm : System.Web.UI.Page
    {
        #region  Variable & constant
        int lScreenMode = 0; //-1 = AddNew, 0 = View, 1 = Edit and 2 = delete.
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        #endregion Public Methods

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            ClearScreen();
            if (CheckUserAutherization())
            {
                InitializeScreen();

            }
            else
            {
                //Response.Redirect("~/Pages/AccessDenied.aspx");
            }


        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.dlsUserList.Enabled = true;
            this.DivAddNewScocietyComm.Visible = true;
            lScreenMode = -1; //AddNew Mode


        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            this.DivAddNewScocietyComm.Visible = true;
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            
        }
        
        #endregion Events

        #region Private Methods
        private bool CheckUserAutherization()
        {
            bool IsAuthrized = false;
            if (Session["UserContext"] != null)
            {
                UserContext UserSession = (UserContext)(Session["UserContext"]);
                if (UserSession.CurrentRole != null)   //This will happen only for registerd guest user
                {
                    switch (UserSession.CurrentRole.RoleCode)
                    {
                        case "SYSSPRADM":
                            IsAuthrized = false;
                            break;
                        case "SYSADMIN":
                            IsAuthrized = false;
                            break;
                        case "SYSOPRAT":
                            IsAuthrized = false;
                            break;
                        case "SCTSUPADM":
                            IsAuthrized = true;
                            break;
                        case "SCTADMIN":
                            IsAuthrized = true;
                            break;
                        case "SCTOPRAT":
                            IsAuthrized = false;
                            break;
                        case "APTBLDR":
                            IsAuthrized = false;
                            break;
                        case "FLTOWNER":
                            IsAuthrized = false;
                            break;
                        case "FLTRESI":
                            IsAuthrized = false;
                            break;
                        default:
                            IsAuthrized = false;
                            break;
                    }
                }
            }
            return IsAuthrized;
        }

        private void FillSocietyDetails(Society pSociety)
        {
            this.lblSocietyName.Text = pSociety.SocietyName;
            this.lblSocietyCode.Text = pSociety.SocietyCode;
            this.lblSocietyAddressLine1.Text = pSociety.SocietyAddressLine1;
            this.lblSocietyAddressLine2.Text = pSociety.SocietyAddressLine2;
            this.lblSocietyCity.Text = pSociety.SocietyAddressCity;
            this.lblSocietyState.Text = pSociety.SocietyAddressState;
        }

        private void FillGridWithExistingCommitteeMemberDetails()
        {
            try
            {
                if (Session["UserContext"] != null)
                {
                    UserContext UserSession = (UserContext)(Session["UserContext"]);
                    if (UserSession.CurrentSociety != null)   //This will happen only for registerd guest user
                    {
                        SocietyManagementCommitteeRegistrationBus lSocietyManagementCommitteeRegistrationBus = new SocietyManagementCommitteeRegistrationBus();
                        List<SocietyManagementMemberRegister> lSocietyManagementMembers = lSocietyManagementCommitteeRegistrationBus.GetAllSocietyManagementCommitteeMemberForSelectedSociety(UserSession.CurrentSociety.SocietyKey);
                        List<SocietyManagementCommitteeMember> lSocietyManagementCommitteeMembers = new List<SocietyManagementCommitteeMember>();
                        foreach (SocietyManagementMemberRegister lSocietyManagementMember in lSocietyManagementMembers)
                        {
                            User lTempUser = lSocietyManagementCommitteeRegistrationBus.GetSelectedUser(lSocietyManagementMember.MemberUserKey);
                            SocietyManagementCommitteeMember tempSocietyManagementCommitteeMember = new SocietyManagementCommitteeMember();

                            tempSocietyManagementCommitteeMember.SocietyKey = lSocietyManagementMember.MemberSocietyKey;
                            tempSocietyManagementCommitteeMember.StartDateOfMembership = lSocietyManagementMember.StartDateOfMembership;
                            tempSocietyManagementCommitteeMember.EndDateOfMembership = lSocietyManagementMember.EndDateOfMembership;

                            tempSocietyManagementCommitteeMember.UserKey = lTempUser.UserKey;
                            tempSocietyManagementCommitteeMember.UserLoginID = lTempUser.UserLoginID;
                            tempSocietyManagementCommitteeMember.UserFirstName = lTempUser.UserFirstName;
                            tempSocietyManagementCommitteeMember.UserLastName = lTempUser.UserLastName;
                            tempSocietyManagementCommitteeMember.UserPrimaryEmailID = lTempUser.UserPrimaryEmailID;

                            UserProfile lTempUserProfile = lSocietyManagementCommitteeRegistrationBus.GetSelectedUserProfile(lSocietyManagementMember.MemberUserKey);
                            tempSocietyManagementCommitteeMember.UserFullName = lTempUserProfile.UserFullName;
                            tempSocietyManagementCommitteeMember.UserSex = lTempUserProfile.UserSex;
                            tempSocietyManagementCommitteeMember.UserContactNumber = lTempUserProfile.UserContactNumber;



                            lSocietyManagementCommitteeMembers.Add(tempSocietyManagementCommitteeMember);
                        }
                        this.gdvCommitteeMembersList.DataSource = lSocietyManagementCommitteeMembers;
                        this.gdvCommitteeMembersList.DataBind();
                    }
                }

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
            }
        }

        private void ClearScreen()
        {
            this.lblSocietyCode.Text = string.Empty;
            this.lblSocietyName.Text = string.Empty;
            this.lblSocietyAddressLine1.Text = string.Empty;
            this.lblSocietyAddressLine2.Text = string.Empty;
            this.lblSocietyCity.Text = string.Empty;
            this.lblSocietyState.Text = string.Empty;
            this.gdvCommitteeMembersList.DataSource = null;
            this.gdvCommitteeMembersList.DataBind();
            this.dlsUserList.Items.Clear();
            this.txtStartDate.Text = string.Empty;
            this.txtEndDate.Text = string.Empty;

        }

        private void InitializeScreen()
        {
            //For society details
            if (Session["UserContext"] != null)
            {
                UserContext UserSession = (UserContext)(Session["UserContext"]);
                if (UserSession.CurrentSociety != null)   //This will happen only for registerd guest user
                {
                    FillSocietyDetails(UserSession.CurrentSociety);
                }
            }

            //For Listing of existing committe members

            FillGridWithExistingCommitteeMemberDetails();
            this.dlsUserList.Enabled = false;

        }

        private void FillLookUpDataForUserList()
        {
           
        }

        private void SelectedCommitteeMemberDetails(User pUser)
        {
            this.lblUserLoginId.Text = pUser.UserLoginID;
            this.lblUserFirstName.Text = pUser.UserFirstName;
            this.lblUserLastName.Text = pUser.UserLastName;
            this.lblUserEmailId.Text = pUser.UserPrimaryEmailID;
        }

        private void FillDropDownUserList()
        {
            SocietyManagementCommitteeRegistrationBus lSocietyManagementCommitteeRegistrationBus = new SocietyManagementCommitteeRegistrationBus();
            
            
        }
        #endregion Private Methods


    }
}